﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using YaP.Library.Extensions;

namespace YaP.Library
{
    internal class AliasesFactory
    {
        //private Dictionary<string, AliasDesc> AliasDictionnary;
        private SerializableDictionary<string, AliasDesc> AliasDictionnary;

        public AliasesFactory()
        {
            AliasDictionnary = new SerializableDictionary<string, AliasDesc>();

            AliasDesc ServerIn = new AliasDesc();
            ServerIn.Type = AliasType.Internal;
            AddToDictionnary("SERVER", ServerIn);
        }

        public bool SaveToXML(string path)
        {
            if (path == string.Empty)
                throw new ArgumentException("File Path cannot be empty.", "path");
            XmlSerializer xmls = new XmlSerializer(typeof (SerializableDictionary<string, AliasDesc>));
            try
            {
                XmlTextWriter TextWriter = new XmlTextWriter(path, Encoding.UTF8);
                TextWriter.Indentation = 4;
                TextWriter.Formatting = Formatting.Indented;
                xmls.Serialize(TextWriter, AliasDictionnary);
            }
            catch (System.IO.IOException ex)
            {
                return false;
            }

            return true;
        }

        public void LoadFromXML(string path)
        {
            XmlSerializer xmls = new XmlSerializer(AliasDictionnary.GetType());
            XmlTextReader TextReader = new XmlTextReader(path);
            SerializableDictionary<string, AliasDesc> dict =
                xmls.Deserialize(TextReader) as SerializableDictionary<string, AliasDesc>;
            AliasDictionnary = dict;
        }

        public bool IsAlias(string cmd, out AliasDesc Description)
        {
            return AliasDictionnary.TryGetValue(cmd, out Description);
        }

        public void AddToDictionnary(string key, AliasDesc value)
        {
            key = key.ToUpper();
            if (value.Expression != null)
                value.Expression = value.Expression.ToUpper();
            try
            {
                AliasDictionnary.Add(key, value);
            }
            catch (Exception)
            {
                AliasDictionnary.Remove(key);
                AliasDictionnary.Add(key, value);
            }
        }

        public bool RemoveFromDictionnary(string key)
        {
            return AliasDictionnary.Remove(key);
        }

        public string AliasToRFC(string command, string[] arguments)
        {
            AliasDesc val;
            if (AliasDictionnary.TryGetValue(command, out val))
            {
                arguments = arguments.DeleteEmptyCells();
                int ArgumentsNeeded = val.Expression.CountChars('$');
                if (ArgumentsNeeded != arguments.Length)
                {
                    return string.Empty;
                }
                string RfcMsg = val.Expression;
                for (int i = 0; i < arguments.Length; i++)
                {
                    RfcMsg = RfcMsg.Replace("$" + (i + 1), arguments[i]);
                }
                return RfcMsg;
            }
            return string.Empty;
        }
    }
}